package com.offcn.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.offcn.pojo.Checkitem;
import com.offcn.service.CheckitemService;
import com.offcn.utils.MessageConstant;
import com.offcn.utils.PageResult;
import com.offcn.utils.QueryPageBean;
import com.offcn.utils.Result;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @Author: wy
 * @Date: 2022/8/29 15:40
 */
@RestController
@RequestMapping("/checkitem")
public class CheckitemController {

    private Result result;

    @Reference
    private CheckitemService checkitemService;

    @RequestMapping("/add")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD','CHECKITEM_EDIT')")
    private Result add(@RequestBody Checkitem checkitem) {

        try {
            checkitemService.add(checkitem);
            result = new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS, null);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL, null);
            throw new RuntimeException(e);
        }
        return result;
    }


    @RequestMapping("/findPage")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkitemService.findPage(queryPageBean);
    }


    //编辑 根据id 进行显示内容当
    @RequestMapping("/updatelist")
    public Checkitem updatelist(@RequestBody Checkitem checkitem) {
        Checkitem checkitem1 = checkitemService.updatelist(checkitem);
        return checkitem1;
    }


    //修改 根据id 进行修改
    @RequestMapping("updateInfoById")
    public Result updateInfoById(@RequestBody Checkitem checkitem) {
        try {
            checkitemService.updateInfoById(checkitem);
            result = new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS, null);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL, null);
            throw new RuntimeException(e);
        }
        return result;
    }

    //删除 根据检查项的id 进行删除检查项
    @RequestMapping("/deleteInfoByid")
    @Secured({"ROLE_ADMIN"})
    public Result deleteInfoByid(@RequestParam(value = "id") Integer id) {
        try {
            checkitemService.deleteInfoByid(id);
            result = new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS, null);
        } catch (Exception e) {
            result = new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL, null);
            throw new RuntimeException(e);
        }
        return result;

    }


    //查询检查项的方法
    @RequestMapping("/showAllItem")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public List<Checkitem> showAllItem() {
        return checkitemService.showAllItem();
    }


    //根据id进行查询 到检查组下的 检查 事项
    @RequestMapping("/getCheckItemIdByGroupId")
    public List<Integer> getCheckItemIdByGroupId(@RequestParam(value = "id") Integer id){
        return checkitemService.getCheckItemIdByGroupId(id);
    }

}
